package com.aiti.lulian.utils;

import com.aiti.lulian.entity.LuLianDockingMeetingDetails;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class ActivityExcelExportUtil {

    public static void exportExcel(HttpServletResponse response, List<LuLianDockingMeetingDetails> modelList) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("活动列表");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("产业链");
        headerRow.createCell(1).setCellValue("龙头企业");
        headerRow.createCell(2).setCellValue("举办时间");
        headerRow.createCell(3).setCellValue("举办地点");
        headerRow.createCell(4).setCellValue("负责单位");

        // 填充数据
        int rowNum = 1;
        for (LuLianDockingMeetingDetails activity : modelList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(activity.getIndustryChain());
            row.createCell(1).setCellValue(activity.getTopCompany());
            row.createCell(2).setCellValue(activity.getMeetingDateStr());
            row.createCell(3).setCellValue(activity.getExportAddress());
            row.createCell(4).setCellValue(activity.getHeadUnit());
        }

        // 自动调整列宽
        for (int i = 0; i < 5; i++) {
            sheet.autoSizeColumn(i);
        }

        String filename = "活动列表数据.xlsx";
        // 设置下载响应头
        response.setContentType("application/msexcel");//application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
        response.setHeader("Content-disposition", "attachment; filename=" + ActivityCaiGouExcel.toUtf8String(filename));//Content-Disposition

        // 输出Excel文件
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
    }
}
